﻿using blackhouse.Translator.Business;
using blackhouse.WebNemo.Business.Translate;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace blackhouse.WebNemo.Translator {

    internal class WebNemoTranslatorEditor : IResourceEditor {

        #region Fields

        private blackhouse.WebNemo.Business.Translate.Translator translateSupport;
        private Dictionary<TranslateKeyInfo, string> translatesToSave = new Dictionary<TranslateKeyInfo, string>();
        private CultureInfo currentLanguage;

        #endregion

        #region Constructors

        public WebNemoTranslatorEditor(CultureInfo language, blackhouse.WebNemo.Business.Translate.Translator translatorProvider) {
            this.translateSupport = translatorProvider;
            this.currentLanguage = language;
        }

        #endregion

        #region Methods

        public void Save() {
            foreach (KeyValuePair<TranslateKeyInfo, string> item in this.translatesToSave) {
                this.translateSupport.SetText(this.currentLanguage, item.Key.ColumnId, item.Key.RecordId, item.Value);
            }
            this.translatesToSave.Clear();
        }

        public void SetTranslatedText(string resourceKey, string text) {
            TranslateKeyInfo keyInfo = TranslateKey.ParseKey(resourceKey);
            this.translatesToSave.Add(keyInfo, text);
        }

        public void Dispose() {
        }

        #endregion

    }
}
